package com.project.XXXX.api;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.project.XXXX.inter.tbl_dictMapper;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.aldjob.controllerImpl.EmployeePtMangaeController;
import com.aldjob.controllerImpl.EmployerPtMangaeController;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.project.XXXX.controllerInterface.IJobManager;
import com.project.XXXX.identification.tool.ValidateTool;
import com.project.XXXX.inter.tbl_divisionMapper;
import com.project.XXXX.inter.tbl_employeeinfoMapper;
import com.project.XXXX.inter.tbl_employerinfoMapper;
import com.project.XXXX.inter.tbl_jobenrollMapper;
import com.project.XXXX.inter.tbl_ptjobMapper;
import com.project.XXXX.inter.tbl_sysuserMapper;
import com.project.XXXX.inter.tbl_workplaceMapper;
import com.project.XXXX.inter.tbl_worktimeMapper;
import com.project.XXXX.model.tbl_employeeinfo;
import com.project.XXXX.model.tbl_jobenroll;
import com.project.XXXX.model.tbl_ptjob;
import com.project.XXXX.model.tbl_ptjobWithBLOBs;
import com.project.XXXX.model.tbl_sysuser;
import com.project.XXXX.model.tbl_workplace;
import com.project.XXXX.model.tbl_worktime;
import com.project.XXXX.stringmodel.EEinfoAndjobString;
import com.project.XXXX.stringmodel.PtJobAddress;
import com.project.XXXX.stringmodel.PtJobForShown;
import com.project.XXXX.stringmodel.PtJobTime;
import com.project.XXXX.tools.ProjectTools;

@Controller
public class EmployerManage implements IJobManager {
	@Resource
	private tbl_ptjobMapper tbl_ptjobMapper;
	@Resource
	private tbl_sysuserMapper tbl_sysuserMapper;
	@Resource
	private tbl_worktimeMapper tbl_worktimeMapper;
	@Resource
	private tbl_dictMapper tbl_dictMapper;
	@Resource
	private tbl_jobenrollMapper tbl_jobenrollMapper;
	@Resource
	private tbl_divisionMapper tbl_divisionMapper;
	@Resource
	private tbl_workplaceMapper tbl_workplaceMapper;
	@Resource
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	@Resource
	private tbl_employerinfoMapper tbl_employerinfoMapper;

	EmployerPtMangaeController uc;
	boolean first;

	void prepare(HttpServletResponse response) {
		if (first) {
			// System.out.println("is first");
			uc = new EmployerPtMangaeController(tbl_divisionMapper, tbl_employeeinfoMapper, tbl_jobenrollMapper, tbl_worktimeMapper, tbl_ptjobMapper, tbl_dictMapper, tbl_workplaceMapper, tbl_employerinfoMapper);
			first = false;
		}
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	}

	HttpSession getAppSession(String vaidatecodeid) {
		SessionContext sessionContext = SessionContext.getInstance();
		return sessionContext.getSession(vaidatecodeid);
	}

	public EmployerManage() {
		first = true;
	}

	@RequestMapping(value = "/api/employerManage/Post")
	public void PostJob(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session,@RequestParam("access_token") String sessonid) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		int status = uc.Post(request, sess);
		switch (status) {
		case 200:
			map.put("status", "success");
			break;
		case 403:
			map.put("status", "failure");
			map.put("error_description", "please log in");
			break;
		}
		response.setStatus(status);
		ProjectTools.ResponseOut(response, new Gson().toJson(map));
	}

	@RequestMapping("/api/employerManage/getCountbyStatus")
	public void showERPtJobsCount(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session, @RequestParam("access_token") String sessonid) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		int s = uc.Post(request, sess);
		if (s == 403) {
			map.put("status", "failure");
			map.put("error_description", "please log in");
			response.setStatus(403);
		} else {
			map.put("status", "success");
		}

		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);

	}

	@RequestMapping("/api/employerManage/getbyStatus")
	public void showERPtJobs(HttpServletRequest request, HttpServletResponse response, Model model,
			@RequestParam("access_token") String sessonid, @RequestParam("status") String status,
			@RequestParam("page") String page, HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		List<PtJobForShown> s = uc.getbyStatus(Integer.parseInt(page), status, sess);
		String json = null;
		if (s == null) {
			map.put("status", "failure");
			map.put("error_description", "please log in");
			response.setStatus(403);
			json = new Gson().toJson(map);
		} else {
			json = new Gson().toJson(s);
		}

		ProjectTools.ResponseOut(response, json);

	}

	@RequestMapping(value = "/api/employerManage/getDetail")
	public void editPtJob(@RequestParam("id") int id, HttpServletRequest request, HttpServletResponse response,
			Model model, HttpSession session, @RequestParam("access_token") String sessonid) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		String s = uc.getDetail(id, sess);
		if (s == null) {
			map.put("status", "failure");
			map.put("error_description", "please log in");
			response.setStatus(403);
			s = new Gson().toJson(map);
		}

		ProjectTools.ResponseOut(response, s);
	}

	@RequestMapping(value = "/api/employerManage/delete")
	public void deletePtJob(@RequestParam("id") int id, HttpServletRequest request, HttpServletResponse response,
			Model model, HttpSession session, @RequestParam("access_token") String sessonid) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		int status = uc.deletePtJob(id, sess);
		switch (status) {
		case 200:
			map.put("status", "success");
			break;
		case 403:
			map.put("status", "failure");
			map.put("error_description", "please log in");
			break;
		case 409:
			map.put("status", "failure");
			map.put("error_description", "not your pt");
			break;
		}
		response.setStatus(status);

		ProjectTools.ResponseOut(response, new Gson().toJson(map));
	}


	@RequestMapping(value = "/api/employerManage/start")
	public void changeJobStatus(@RequestParam("id") int ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		int status = uc.changestatus(28, ptid, sess);//进行中
		switch (status) {
		case 200:
			map.put("status", "success");
			break;
		case 403:
			map.put("status", "failure");
			map.put("error_description", "please log in");
			break;
		case 409:
			map.put("status", "failure");
			map.put("error_description", "not your pt");
			break;
		}
		response.setStatus(status);
		ProjectTools.ResponseOut(response, new Gson().toJson(map));
	}
	
	@RequestMapping(value = "/api/employerManage/ackPass")
	public void changeJobStatus1(@RequestParam("id") int ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		int status = uc.changeJobStatus(31, ptid, sess);//审核通过
		switch (status) {
		case 200:
			map.put("status", "success");
			break;
		case 403:
			map.put("status", "failure");
			map.put("error_description", "please log in");
			break;
		case 409:
			map.put("status", "failure");
			map.put("error_description", "not your pt");
			break;
		}
		response.setStatus(status);
		ProjectTools.ResponseOut(response, new Gson().toJson(map));
	}
	@RequestMapping(value = "/api/employerManage/ackFail")
	public void changeJobStatus2(@RequestParam("id") int ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		int status = uc.changeJobStatus(32, ptid, sess);//审核不通过
		switch (status) {
		case 200:
			map.put("status", "success");
			break;
		case 403:
			map.put("status", "failure");
			map.put("error_description", "please log in");
			break;
		case 409:
			map.put("status", "failure");
			map.put("error_description", "not your pt");
			break;
		}
		response.setStatus(status);
		ProjectTools.ResponseOut(response, new Gson().toJson(map));
	}
	@RequestMapping(value = "/api/employerManage/end")
	public void end(@RequestParam("id") int ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		int status = uc.changestatus(29, ptid, sess);//进行中
		switch (status) {
		case 200:
			map.put("status", "success");
			break;
		case 403:
			map.put("status", "failure");
			map.put("error_description", "please log in");
			break;
		case 409:
			map.put("status", "failure");
			map.put("error_description", "not your pt");
			break;
		}
		response.setStatus(status);

		ProjectTools.ResponseOut(response, new Gson().toJson(map));
	}
	@RequestMapping(value = "/api/employerManage/getAllCount")
	public void getAllCount(@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		int s = uc.getAllCount(sess);
		if (s == -1) {
			map.put("status", "failure");
			map.put("error_description", "please log in");
			response.setStatus(403);
		} else {
			map.put("count", "" + s);
		}

		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "/api/employerManage/getAll")
	public void getAll(@RequestParam("page") int page,@RequestParam("access_token") String sessonid, HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		List<tbl_ptjobWithBLOBs> s = uc.getAll(page,sess);
		String json;
		if (s == null) {
			map.put("status", "failure");
			map.put("error_description", "please log in");
			response.setStatus(403);
			json = new Gson().toJson(map);
		} else {
			json = new Gson().toJson(s);
		}
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping(value = "/api/employerManage/getRequestbyIdandStatus")
	public void getReqUser(HttpServletRequest request, HttpServletResponse response, Model model,
			@RequestParam("access_token") String sessonid, @RequestParam("status") String status,
			@RequestParam("page") int page,  @RequestParam("id") int id,HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		List<EEinfoAndjobString> s = uc.getRequestbyIdandStatus(page, status, id, sess);
		String json = null;
		if (s == null) {
			map.put("status", "failure");
			map.put("error_description", "please log in");
			response.setStatus(403);
			json = new Gson().toJson(map);
		} else {
			json = new Gson().toJson(s);
		}
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping(value = "/api/employerManage/getRequestCountbyIdandStatus")
	public void getReqUserCount(@RequestParam("status") int status,@RequestParam("access_token") String sessonid,
			@RequestParam("id") int ptjobid,HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(sessonid);
		int s = uc.getRequestCountbyIdandStatus(ptjobid,status, sess);
		if (s == -1) {
			map.put("status", "failure");
			map.put("error_description", "please log in");
			response.setStatus(403);
		} else {
			map.put("count", "" + s);
		}

		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);

	}

}
